/*
 * Copyright (c) 2009, Servoy-stuff
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 * http://www.fsf.org/licensing/licenses/lgpl.txt
 */
package net.stuff.servoy.beans.slider;

import com.servoy.j2db.IForm;
import com.servoy.j2db.IFormManager;
import com.servoy.j2db.dataprocessing.IFoundSet;
import com.servoy.j2db.dataprocessing.IRecord;
import com.servoy.j2db.plugins.IClientPluginAccess;
import com.servoy.j2db.util.Debug;

/**
 * 
 * @author Servoy Stuff
 * http://www.servoy-stuff.net/
 */
public class ServoyUtils {

	/**
	 * Test a String
	 * @return true if the passed String is not empty
	 */
	public static boolean isNotEmpty(String s) {
		return (s != null && s.trim().length() > 0);
	}
	
	/**
	 * Test the existence of inited foundset and record
	 */
	public static boolean isExisting(IFoundSet foundset, IRecord record) {
		return (foundset != null && foundset.getSize() > 0 && record != null);
	}
	
	/**
	 * Test if the dataProvider of function name is a global (starting with globals.)
	 */
	public static boolean isGlobal(String s) {
		return (s != null && s.trim().toLowerCase().startsWith("globals."));
	}

	
	/**
	 * Test method for the validity of a dataprovider
	 * @param record the currentRecord
	 * @param dataProviderID the dataProviderID
	 * @return true id the dataProvider exists in the current FoundSet
	 */
	public static boolean isContainedInFoundset(IRecord record, String dataProviderID) {
		boolean isContained = false;
		if (record != null && isNotEmpty(dataProviderID)) {
			try {
				if (isGlobal(dataProviderID)) {
					isContained = record.getParentFoundSet().containsDataProvider(dataProviderID);
				} else {
					if (dataProviderID.indexOf(".") < 0) {
						isContained = record.has(dataProviderID);
					} else {
						// no way to know for sure if the dataProvider is valid in related records.
						// so let's consider it is - it will not harm since calls to
						// getValue() or setValue() on an IRecord will not throw Exceptions!
						isContained = true; 
					}
				}
			} catch (Exception ex) {
				Debug.error(ex);
			}
		}
		return isContained;
	}
	
	public static boolean booleanEquals(boolean a, Boolean b) {
		return (b != null && a == b.booleanValue());
	}
	
	public static boolean intEquals(int a, Integer b) {
		return (b != null && a == b.intValue());
	}
	
	public static boolean doubleEquals(double a, Double b) {
		return (b != null && a == b.doubleValue());
	}
	
	public static boolean numberEquals(Number a, Number b) {
		if (a == null && b == null) return true;
		if (a == null || b == null) return false;
		return (a.doubleValue() == b.doubleValue());
	}
	
	/**
	 * Generic helper callback function
	 * 
	 * @param functionName
	 * @param app
	 * @param params
	 */
	public static void runFunctionByName(String functionName, IClientPluginAccess app, Object[] params) {
		if (functionName != null) {
			String fName = functionName.trim();
			if (fName.length() > 0) {
				if (isGlobal(fName)) {
					runGlobalFunctionByName(fName, app, params);
				} else {
					runFormFunctionByName(fName, app, params);
				}
			}
		}
	}
	
	/**
	 * Generic callback to a "global" Function
	 * @param fName
	 * @param app
	 * @param params
	 */
	private static void runGlobalFunctionByName(String fName, IClientPluginAccess app, Object[] params) {
		try {
			fName = fName.substring("globals.".length());
			app.executeMethod(null, fName, params, false);
		} catch (Exception ex) {
			Debug.error(ex);
		}
	}
	
	
	/**
	 * Generic callback to a "form" Function
	 * 
	 * @param fName
	 * @param app
	 * @param params
	 */
	private static void runFormFunctionByName(String fName, IClientPluginAccess app, Object[] params) {
		IFormManager manager = app.getFormManager();
		if (manager != null) {
			IForm form = null;
			int indexOfDot = fName.indexOf('.');
			if (indexOfDot > -1) {
				form = manager.getForm(fName.substring(0, indexOfDot));
				fName = fName.substring(indexOfDot+1);
			} else {
				form = manager.getCurrentForm();
			}
			if (form != null) {
				String formName = form.getName();
				if (formName != null) {
					try {
						app.executeMethod(formName, fName, params, false);
					} catch (Exception ex) {
						Debug.error(ex);
					}
				}
			}
		}
	}


}
